<template>
  <!-- 模板盒子 -->
  <div class="LoginModal">
    <!-- 遮罩盒子 -->
    <div class="mask" @click="changeisShowLoginMadal(false)"></div>
    <!-- 登录盒子 -->
    <div class="LoginBox">
      <!-- 点击取消登录 -->
      <span class="close" @click="changeisShowLoginMadal(false)"></span>
      <!-- 导航 -->
      <div class="nav">
        <a href="#" :class="{ active: isShow }" @click="isShow = true"
          >手机号码登录</a
        >
        |
        <a href="#" :class="{ active: !isShow }" @click="showQRcode"
          >微信扫码登录</a
        >
      </div>
      <!-- 内容 -->
      <div class="centent">
        <div class="phoneCentent">
          <!-- 手机号码登录内容 -->
          <div v-show="isShow">
            <div class="row">
              <input
                type="text"
                placeholder="请输入手机号"
                class="ipt"
                v-model="phoneNumber"
              />
            </div>
            <div class="row">
              <slide-verify
                :l="42"
                :r="20"
                :w="362"
                :h="140"
                @success="onSuccess"
                @fail="onFail"
                @refresh="onRefresh"
                :style="{ width: '100%' }"
                class="slide-box"
                ref="slideBlock"
                :slider-text="msg"
                :imgs="arr"
              ></slide-verify>
            </div>
            <!-- <div>{{ msg }}</div> -->
            <div class="row">
              <input
                type="text"
                placeholder="请输入验证码"
                class="ipt"
                v-model="value"
              />
              <!-- <button class="getcode" @click="getCode">获取验证码</button> -->
              <button v-show="showCount" class="getcode" @click="getCode">
                获取验证码
              </button>
              <button v-show="!showCount" class="getcode">
                {{ count + "s" }}
              </button>
            </div>
            <button class="btnLogin" @click="submitFn">登录</button>
          </div>
        </div>
        <!-- 微信扫码登录 -->
        <div id="weixin" class="qrcode" v-show="!isShow">二维码</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import { sendSMSAPI, PhoneReginAPI, BindingAPI } from "@/request/api";
import { validataPhoneNumber } from "@/utils/index";
export default {
  data() {
    return {
      isShow: true,
      msg: "向右滑动",
      count: 59,
      showCount: true,
      arr: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-02.150d92a1.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-04.afadcbdf.png"),
      ],
      phoneNumber: "",
      value: "",
    };
  },

  mounted() {},

  methods: {
    ...mapMutations({
      changeisShowLoginMadal: "showMadal/changeisShowLoginMadal",
      changIsShowLogin: "showLogin/changIsShowLogin",
      // changeIsShowToastMadal: "showToast/changeIsShowToastMadal",
    }),
    ...mapActions({
      asyncChangeIsShowToastMadal: "showToast/asyncChangeIsShowToastMadal",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },

    // 倒计时
    daojishi() {
      this.showCount = false;
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 59;
          this.showCount = true;
          clearInterval(timer);
        }
      }, 1000);
    },
    // 判断手机号和验证码
    toverify() {
      // 判断手机号
      if (!validataPhoneNumber(this.phoneNumber)) {
        this.asyncChangeIsShowToastMadal({
          show: true,
          msg: "请输入正确的手机号码",
          type: "info",
        });
        return;
      }
      // 判断手机号码的正则表达式是否正确
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        this.asyncChangeIsShowToastMadal({
          show: true,
          msg: "请移动滑块",
          type: "info",
        });
        return;
      }
      return true;
    },
    // 获取验证码
    // 发送请求   判断移滑块是否成功
    async getCode() {
      //发送请求
      if (!this.toverify()) return;
      let res = await sendSMSAPI({
        phone: this.phoneNumber,
      });
      if (!res) return;
      // 验证码发送成功
      this.asyncChangeIsShowToastMadal({
        show: true,
        msg: "发送成功",
        type: "success",
      });
      // 调用倒计时
      this.daojishi();
    },
    // 登录
    async submitFn() {
      // 1.判断手机号码
      // 2.判断滑块验证
      if (!this.toverify()) return;
      // 3.判断短信验证码
      if (!this.value.trim()) {
        return this.asyncChangeIsShowToastMadal({
          show: true,
          msg: "请输入短信验证码",
          type: "info",
        });
      }

      // return alert("请输入短信验证码");
      // 4.发送请求 两种情况  如果有uuid,就是扫码登录,发送手机和微信绑定请求, 没有则是普通请求
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = await BindingAPI({
          verifyCode: this.value,
          phone: this.phoneNumber,
          uuid,
        });
      } else {
        res = await PhoneReginAPI({
          verifyCode: this.value,
          phone: this.phoneNumber,
        });
      }
      // 5.存储token
      localStorage.setItem("token", res["x-auth-token"]);
      // 5.topbar状态栏的改变
      // 模态框
      this.changeisShowLoginMadal(false);
      // 购物车
      this.changIsShowLogin(true);
      // 删除code
      this.$router.push(this.$route.path);
      // 删除uuid
      localStorage.removeItem("uuid");
      // 提示登录成功
      this.asyncChangeIsShowToastMadal({
        show: true,
        msg: "登录成功",
        type: "success",
      });
    },
    // 微信扫码登录
    showQRcode() {
      this.isShow = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.LoginModal {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  .mask {
    width: 100%;
    height: 100%;
    position: relative;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.5);
  }
  .LoginBox {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      top: 10px;
      right: 60px;
      display: block;
      width: 22px;
      height: 22px;
      background: url("../assets/img/close.png");
    }
    .nav {
      text-align: center;
      padding-top: 30px;
      a {
        text-decoration: none;
        color: gray;
        &.active {
          color: black;
        }
      }
    }
    .centent {
      width: 360px;
      height: 270px;
      //   background-color: palegreen;
      margin: 30px auto;
      .row {
        display: flex;
        margin-bottom: 20px;
        .ipt {
          flex: 1;
          height: 50px;
          outline: none;
          text-indent: 1em;
        }
        .getcode {
          width: 90px;
          margin-left: 10px;
          border: none;
          background-color: #0a328e;
          color: #fff;
        }
      }
      .btnLogin {
        width: 100%;
        height: 50px;
        border: none;
        background-color: #0a328e;
        color: #fff;
      }
      .qrcode {
        text-align: center;
      }
    }
  }
}
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>